<?php
//开启xhprof
if(function_exists('xhprof_enable')) {
    xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
//    register_shutdown_function(function() {
//        file_put_contents('log/xhprof-' . date('YmdH') . '.log', serialize(xhprof_disable()), FILE_APPEND);
//    });
}

$_DUDULOOK = array(
	'IMPORT' => array(),
	'WEBCONFIG' => '',//require(DUDULOOK_PATH . 'web.config.php'),
    	'c' => '',
);
//if(!defined('PROJECT_PACKAGE')) define('PROJECT_PACKAGE', '');
if(!defined('DEV_MODE')) define('DEV_MODE', 'LOCAL'); //LOCAL,DEV,WWW
if(!defined('ACTION_BASE_PATH')) define('ACTION_BASE_PATH', 'action');

function import($path)
{
	$path = trim($path);
	if(!$path) throw new Exception('import($path) exception, $path empty');
	if(strpos($path, '~') === 0) $path = 'com.dudulook.' . substr($path, 1, strlen($path) - 1);
	
	$suffix = array('class'=>'.class.php', 'func'=>'.func.php', 'interface'=>'.interface.php', 'config'=>'.config.php');

	global $_DUDULOOK;
	$keys = array_keys($_DUDULOOK['IMPORT']);
	if(in_array($path, $keys)) 
	{
		if($_DUDULOOK['IMPORT'][$path]['suffix'] == 'config') return include $_DUDULOOK['IMPORT'][$path]['fullDir'] ;
		return false;
	}
	
	$res = array();
	$pathArr = explode('.', $path);
	$res['name'] = array_pop($pathArr);
	$res['path'] = implode('.', $pathArr);
	if(strpos($path, 'com.dudulook') !== false)//dudulook
	{
		unset($pathArr[0]);
		unset($pathArr[1]);
		$pathString = implode('/', $pathArr);
		$res['dir'] = DUDULOOK_PATH .($pathString ? "$pathString/" : "");
		$res['fullDir'] = $res['dir'] . $res['name'] . ".class.php";
	}
	else
	{
		$pathString = implode('/', $pathArr);
		$res['dir'] = PROJECT_SRC_PATH . ($pathString ? "$pathString/" : "");
		$res['fullDir'] = $res['dir'] . $res['name'] . ".class.php";
	}
	
	$fullDir = "";
	foreach($suffix as $k => $v)
	{
		$res['fullDir'] = $res['dir'] . $res['name'] . $v;
		if(file_exists($res['fullDir']))
		{
			$fullDir = $res['fullDir'];
			$res['suffix'] = $k;
			break;
		}
	}
	if(!$fullDir) throw new Exception("$path not exist");
	
	$_DUDULOOK['IMPORT'][$path] = $res;
	$res = null;
	unset($res);
	if($_DUDULOOK['IMPORT'][$path] ['suffix'] == 'config')
	{
		return include $_DUDULOOK['IMPORT'][$path] ['fullDir'];
	}
	else
	{
		include $_DUDULOOK['IMPORT'][$path] ['fullDir'];
	}
}

try
{
	import('com.dudulook.func.system');
	import('com.dudulook.lib.core.ClassFactory');
	import('com.dudulook.base.factory.PageContextFactory');
	import('com.dudulook.base.factory.ActionRouterFactory');
	import('com.dudulook.base.factory.ConfigerFactory');
	import('com.dudulook.base.factory.TemplateFactory');
	import('com.dudulook.base.BaseImpl');
	
	if(!defined('HTTP_HOST')) define("HTTP_HOST", $_SERVER['HTTP_HOST']);
	if(!defined('WEB_URL')) define("WEB_URL", getWebUrl());
	
	$pageContext = PageContextFactory::create();
	$actionRouter = ActionRouterFactory::create();
	$actionRouter->execute($pageContext);
	
	//$pageContext->setAttribute('duduLook', );
}
catch(Exception $e1)
{
	echo $e1->getMessage();
}
?>